#pragma once
#include<atomic>
#include"Channel.h"
#include"Callback.h"
namespace wjc {

	class EventChannel:public std::enable_shared_from_this<EventChannel> {
	private:
		Core* core_;
		int eventfd_;
		std::shared_ptr<Channel> channel_;

		OnEventCallback onEvent_;

		int createEventfd();

		void handleRead();

	public:
		EventChannel(Core* core);
		~EventChannel();

		void enable();

		void setOnTimerCallback(OnEventCallback onEvent) {
			onEvent_ = std::move(onEvent);
		}

		void trigger();

		void destruction() {
			auto guard = shared_from_this();
			if (core_->isInCoreThread()) {
				channel_->disableAll();
				channel_->remove();
			}
			else {
				core_->runInCore(CreateTask(std::bind(&EventChannel::destruction, guard)));
			}
		}
	};
}

